﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyUtils.Core
{
    /// <summary>
    /// c语言位图工具
    /// </summary>
    public class CbitMap
    {

        /// <summary>
        /// 转化为bitmap数据数组
        /// eg: arr = ToBit(Enum.GetNames(typeof(Power)));
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static int[] ToBit(string[] arr)
        {
            int[] ret = new int[arr.Length];
            int value = 0b1; // 0x1
            int count = 0;
            for (int i = 0 ; i < arr.Length; i++)
                ret[i] = (value << (1 * count++));
            return ret;
        }
        #region 检查权限
        public static bool Check(int powers, int power)
        {
            return (powers & power) != 0;
        }

        public static bool[] Check(int powers, int[] powerArr)
        {
            bool[] result = new bool[powerArr.Length];
            for (int i = 0; i < powerArr.Length; i++)
                result[i] = ((powers & powerArr[i]) != 0);
            return result;
        }
        #endregion

        #region 添加权限产生新权限
        public static int Add(int powers, int power)
        {
            return (powers |= power);
        }

        public static int Add(int powers, int[] powerArr)
        {
            int newPowers = powers;
            for (int i = 0; i < powerArr.Length; i++)
                newPowers |= powerArr[i];

            return newPowers;
        }
        #endregion

        #region 移除权限
        public static int Remove(int powers, int power)
        {
            return (powers &= ~power);
        }

        public static int Remove(int powers, int[] powerArr)
        {
            int newPowers = powers;
            for (int i = 0; i < powerArr.Length; i++)
                newPowers &= ~powerArr[i];
            return newPowers;
        }
        #endregion
    }
}
